#pragma once

#include "Widget.h"
#include "Window.h"
#include "Types.h"

namespace My {

class TextControl;

class InplaceEdit : 
	public Window<CEdit>, 
	public virtual Widget 
{
private:
	TextControl	&	m_Owner;
public:
	InplaceEdit(TextControl & t);
	~InplaceEdit();
public:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	afx_msg void OnKillFocus(CWnd* pNewWnd);
	afx_msg void OnChange();
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	DECLARE_MESSAGE_MAP()
public:
	virtual CRect Bounds() const
		{ return Widget::Bounds(); }
	virtual bool Visible() const
		{ return Widget::Visible(); }
	virtual const CFont * Font() const
		{ return Widget::Font(); }
	virtual bool CanGetFocus() const
		{ return true; }
	void SetFont(CFont * pFont);
public:
	String GetText();
	void SetText(const String &);
};

class TextControl : 
	public Container, 
	public virtual Widget  
{
	friend class InplaceEdit;
private:
	HCURSOR			m_hCurHandClick;
	InplaceEdit *	m_pEdit;
	CSize			m_Preferred;
	bool			m_TightBounds;	///< true if the bounds of the widget are too tight for the the text to be fully printed,
							///< hint with the control's text should be shown in that case
	bool			m_ReadOnly;
protected:
	CRect			m_TextRect;
protected:
	virtual bool GetText(String & s) = 0;
	virtual void SetText(const String & s) = 0;
	void OpenInplaceEdit();
	void CloseInplaceEdit();
	InplaceEdit * GetInplaceEdit() const
		{ return m_pEdit; }
	CRect CalcTextRect(const String & text);
	void SetPreferredSize(CSize s);
	void UpdatePreferredSize();
public:
	TextControl();
	~TextControl();
public:
	virtual CSize GetPreferredSize() const; 
	virtual String Hint() const;
public:
	void Apply();
	void Cancel();
	virtual bool ReadOnly() const
		{ return m_ReadOnly; }
	void SetReadOnly(bool);
public:
	virtual void LayoutControls();
	virtual void HandlePaint(CDC * pDC, const CRect & r);
	virtual void HandleLeftDown(unsigned int flags, const CPoint & p);
	virtual bool HandleCursor(unsigned int flags, const CPoint & p);
	virtual void HandleVisible(bool on);
	virtual void HandleEnable(bool on);
	virtual void HandleFont();
};

} // My
